<?php
require 'mysql.php';

$origin = 'select stu.sid, 
	soa.stu_selection, 
	oi.question_stem, 
	oi.optionA, 
	oi.optionB, 
	oi.optionC, 
	oi.optionD, 
	oi.right_option 
	from
	student as stu
	left join
	student_objective_anwser as soa on stu.sid=soa.sid
	left join
	objective_item as oi on soa.objitemid = oi.objitemid
	where 
	stu.sid = ?';

if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
	die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
}

$sid = $_SESSION['sid'];
if(!mysqli_stmt_bind_param($mysqli_stmt, 's', $sid)) {
	die('Statement Binding Parameter Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
}

if(!mysqli_stmt_execute($mysqli_stmt)) {
	die('Statement Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
}

if(!mysqli_stmt_bind_result($mysqli_stmt, $sid, $stu_sel, $question_stem, $optA, $optB, $optC, $optD, $right_opt)) {
	die('Statement Result Binding Fail, Error: (' . mysqli_stmt_errno(
		$mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
}

if(!mysqli_stmt_store_result($mysqli_stmt)) {
	die('Statement Storing Result Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
}

$i = 0;
while(mysqli_stmt_fetch($mysqli_stmt)) {
	++$i;
?>

	<div class="item">
		<p><?php printf('%d . %s', $i, $question_stem); ?></p>
		<div class="optgrp">
		<span <?php if('A' == $stu_sel) { ?>style="color:blue" <?php } ?>>A. <?= $optA ?></span>
		<span <?php if('B' == $stu_sel) { ?>style="color:blue" <?php } ?>>B. <?= $optB ?></span>
		</div>
		<div class="optgrp">
		<span <?php if('C' == $stu_sel) { ?>style="color:blue" <?php } ?>>C. <?= $optC ?></span>
		<span <?php if('D' == $stu_sel) { ?>style="color:blue" <?php } ?>>D. <?= $optD ?></span>
		</div>
		<div class="my_anwsr">
		<span style="color:red">答案: <?= $right_opt?></span>
		</div>
	</div>

<?php
}
mysqli_stmt_close($mysqli_stmt);
mysqli_close($mysqli);
?>
